﻿using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using HDJ.Framework.Core;

public class InputManagerWindow : EditorWindow
{
    [MenuItem("Tool/按键配置管理器（Input Manager）")]
    static void DoIt()
    {
        InputManagerWindow win = GetWindow<InputManagerWindow>();
        win.Init();
    }
    Dictionary<string, InputSetData> allInputSetDataDic = new Dictionary<string, InputSetData>();
    private void Init()
    {
        allInputSetDataDic= InputSetDataManager.LoadDataFromFileOnEditor();
    }

    private void OnEnable()
    {
        Init();
    }
    private string keyName = "";

    private Vector2 pos;
    private void OnGUI()
    {

        EditorDrawGUIUtil.DrawFoldout(this, "New", () =>
        {
            keyName = EditorDrawGUIUtil.DrawBaseValue("keyName :", keyName).ToString();
            if (GUILayout.Button("Add"))
            {
                if (!string.IsNullOrEmpty(keyName) && !allInputSetDataDic.ContainsKey(keyName))
                {
                    InputSetData data = new InputSetData();
                    data.key = keyName;
                    allInputSetDataDic.Add(keyName, data);

                    keyName = "";
                }
                else
                {
                    Debug.LogError("keyName 不能为空或重复");
                }
            }
        });

        GUILayout.Space(10);
        pos = GUILayout.BeginScrollView(pos,"box");
        foreach (var item in allInputSetDataDic.Keys)
        {
            DrawInputSetDataGUI(allInputSetDataDic[item]);

            if (GUILayout.Button("Remove"))
            {
                allInputSetDataDic.Remove(item);
                return;
            }
        }
        GUILayout.EndScrollView();
        GUILayout.Space(6);

        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Save"))
        {
            InputSetDataManager.SaveDataOnEditor(allInputSetDataDic);
            AssetDatabase.Refresh();
        }
    }

    private void DrawInputSetDataGUI(InputSetData data)
    {
        EditorDrawGUIUtil.DrawFoldout(data, data.key, () =>
        {
            data.key = EditorDrawGUIUtil.DrawBaseValue("Key :", data.key).ToString();
            data.name = EditorDrawGUIUtil.DrawBaseValue("Name :", data.name).ToString();
            GUILayout.Space(6);
            if (GUILayout.Button("+", GUILayout.Width(30)))
            {
                data.InputKeyTypeDataList.Add(new InputKeyTypeData());
            }
            EditorDrawGUIUtil.DrawScrollView(data.InputKeyTypeDataList, () =>
             {
                 foreach (var item in data.InputKeyTypeDataList)
                 {
                     item.keyType = (InputKeyType)EditorDrawGUIUtil.DrawBaseValue("InputKeyType :", item.keyType);
                     if (item.keyType == InputKeyType.KeyCode)
                     {
                         item.keyboardCode = (KeyCode)EditorDrawGUIUtil.DrawBaseValue("keyboardCode :", item.keyboardCode);
                     }
          

                     if (GUILayout.Button("Remove"))
                     {
                         data.InputKeyTypeDataList.Remove(item);
                         return;
                     }
                     GUILayout.Space(6);
                 }

             }, "box");
           

            GUILayout.Space(10);
        });
     
       
    }
}